package com.mcxx.modules.common.service.impl;

import com.mcxx.common.exception.BizException;
import com.mcxx.modules.common.service.StatisticService;
import com.mcxx.modules.statistics.dto.StatisticDTO;
import com.mcxx.modules.statistics.dto.TempApprovalDTO;
import com.mcxx.modules.statistics.param.StatisticParam;
import com.mcxx.modules.statistics.service.read.StatisticReadService;
import com.mcxx.modules.system.entity.AreaSalvationEntity;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.component.org.orguser.area.entity.TaAreaPo;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Slf4j
@Service
@TaTransactional
public class TempSummaryService extends StatisticService {
    @Autowired
    private StatisticReadService statisticReadService;

    @Override
    public List<? extends StatisticDTO> getStatistic(StatisticParam param, UserAccountVo accountVo, String standType) {
        if(CommonUtil.invalidArgs(param.getMonthEnd()) || param.getMonthEnd().length() != 6) {
            throw new BizException(FailureCode.ERR_10001);
        }

        List<TaAreaPo> taAreaPos = new ArrayList<TaAreaPo>();
        String name = super.commonStatisticStart(param, accountVo, taAreaPos, null);
        if(taAreaPos.isEmpty()){
            throw new BizException(FailureCode.ERR_100002);
        }

        TaAreaPo curTaAreaPo = taAreaPos.get(0);
        String validCode = areaUtil.getValidCode(curTaAreaPo.getAreaCode(), curTaAreaPo.getAreaLevel());

        Integer level = curTaAreaPo.getAreaLevel();
        if (curTaAreaPo.getAreaLevel() < Constant.AreaLevel.COMMUNITY)
            level += 1;
        Integer validCodeLen = areaUtil.getValidCodeLen(curTaAreaPo.getAreaLevel() + 1);
        int initValue = 12;
        if (curTaAreaPo.getAreaLevel() == Constant.AreaLevel.COMMUNITY) {
            initValue = 15;
        }
        String zeros = String.join("", Collections.nCopies(initValue - validCodeLen, "0"));

        String monthStart = param.getMonth();
        monthStart = monthStart.substring(0, 4) + "-" + monthStart.substring(4) + "-01";
        String monthEnd = param.getMonthEnd();
        monthEnd = monthEnd.substring(0, 4) + "-" + monthEnd.substring(4) + "-01";
        name = name + monthEnd.substring(4, 7) + "月临时救助审批汇总表";
        List<TempApprovalDTO> results = statisticReadService.getTempSummary(validCode, validCodeLen, zeros, monthStart, monthEnd, level);
        for(TempApprovalDTO dto : results) {
            dto.setTitle(name);
        }

        return results;
    }

    @Override
    public void fillSalvation(StatisticDTO dto, List<String> standards, List<AreaSalvationEntity> salivations) {

    }
}
